package com.fh.service.blacklist;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
/**
 * 黑名单
 * 
 * @author dell、
 *
 */
import org.springframework.transaction.annotation.Transactional;

import com.fh.mapper.loan.BlackListMapper;
import com.fh.mapper.loan.CallLoanInfoMapper;
import com.fh.model.Page;
import com.fh.model.loan.BlackList;
import com.fh.model.loan.CallLoanInfo;
import com.fh.util.Const;
import com.fh.util.HttpUtils;
import com.fh.util.JsonUtil;
import com.fh.util.PageData;
import com.fh.vo.Fraud;
import com.ybd.common.httpClient.HttpClientUtil;
@Service
public class BlackListServiceImpl implements IBlackListService{
	
	@Autowired
	private BlackListMapper mapper;
	@Autowired
	private CallLoanInfoMapper callLoanInfoMapper;
	private Logger logger = LoggerFactory.getLogger(BlackListServiceImpl.class);
	 //风险调查接口
    @Value("${url.riskInvestigate}")
    private String riskInvestigate;
	 
    //风控黑名单接口
    @Value("${url.riskBlackName}")
    private String riskBlackName;
    
	/**
	 * 提交黑名单
	 */
	@Override
	@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
	public void commitBlackList(BlackList info) throws Exception{
		//查询黑名单是否已经提交过并且审核通过了
		int count = mapper.selectByIdCard(info.getContIdCard());
		if (count > 0) {
			logger.info("查询黑名单的数量===>{}",count);
			throw new RuntimeException("已经提交过黑名单!");
		}
		try {
			CallLoanInfo callLoanInfo = callLoanInfoMapper.queryByCallLoanNid(info.getCallLoanNid());
			if (callLoanInfo != null) {
				info.setContIdCard(callLoanInfo.getContIdCard());//设置身份证号
				info.setTelephone(callLoanInfo.getTelephone());//设置电话号码
			}
			//设置更新创建时间
			info.setCreateTime(new Date());
			info.setUpdateTime(new Date());
			mapper.insert(info);
			
		} catch (Exception e) {
			logger.error("提交黑名单异常",e);
			throw new RuntimeException("保存黑名单失败!");
		}
	}
	/**
	 * 提交欺诈
	 * 
	 */
	@Transactional(readOnly=true)
	public void commitFraud(Fraud info) throws Exception{
		//校验参数不能为null
		//调用风险调查的接口
		if (!StringUtils.isNotBlank(info.getLoanNo())) {
			throw new RuntimeException("借款编号为空!");
		}
		if (!StringUtils.isNotBlank(String.valueOf(info.getChannel()))) {
			throw new RuntimeException("来源渠道为空!");
		}
		if (!StringUtils.isNotBlank(String.valueOf(info.getClueSource()))) {
			throw new RuntimeException("线索来源为空!");
		}
		if (!StringUtils.isNotBlank(String.valueOf(info.getClueType()))) {
			throw new RuntimeException("线索类型为空!");
		}
		if (!StringUtils.isNotBlank(String.valueOf(info.getLoanProduct()))) {
			throw new RuntimeException("借款产品类型为空!");
		}
		if (!StringUtils.isNotBlank(info.getName())) {
			throw new RuntimeException("线索人姓名为空!");
		}
        logger.info("调用风控反欺诈接口url==>{}",riskInvestigate);
        CallLoanInfo callLoanInfo = callLoanInfoMapper.queryByCallLoanNid(info.getCallLoanNid()); 
		if (callLoanInfo != null) {
			info.setIdNumber(callLoanInfo.getContIdCard());//设置身份证号
			info.setMobile(callLoanInfo.getTelephone());//设置电话号码
			if (callLoanInfo.getProductSource() == Const.JRXJ_PRODUCT) {
				//今日现金
				info.setLoanProduct(2);
			}else if(callLoanInfo.getProductSource() == Const.XYKD_PRODUCT){
				//卡贷
				info.setLoanProduct(3);
			}else if(callLoanInfo.getProductSource() == Const.PRO_SOURCE_YOUXI_DAI){
				//游戏贷
				info.setLoanProduct(6);
			}else if(callLoanInfo.getProductSource() == Const.PRO_SOURCE_XIAOFEI_DAI){
				//消费分期
				info.setLoanProduct(7);
			}else if(callLoanInfo.getProductSource() == Const.PRO_SOURCE_R_360){
				//融360
				info.setLoanProduct(8);
			}else if(callLoanInfo.getProductSource() == Const.PRO_SOURCE_JIAOYU_FENQI){
				//教育分期
				info.setLoanProduct(9);
			}/*else if(callLoanInfo.getProductSource() == Const.YXD360_PRODUCT){
				//融360游戏贷
				info.setLoanProduct(8);
			}*/else{
				//其他
				info.setLoanProduct(10);
			}
		}
        logger.info("调用风控反欺诈接口入参==>{}", JSON.toJSON(info.getParameters()));
		String result = HttpUtils.sendPost(riskInvestigate + "/case/insertClueInfo.do", info.getParameters());
        logger.info("调用风控反欺诈接口返回值==>{}",result);
		if (StringUtils.isBlank(result)) {
			throw new RuntimeException("参数异常!");
		}
		if (!"200".equals(JsonUtil.jsonToMap(result).get("code"))) {
			throw new RuntimeException("接口网络异常!");
		}
		
	}
	/**
	 * 黑名单审核列表 
	 */
	@Override
	public List<PageData> selectBlackNamelistPage(Page page) {
		List<PageData> list=mapper.selectBlackNameListPage(page);
		return list;
	}
	/**
	 * 审核通过和拒绝
	 */
	@Override
	public int updateStatus(Map<String, Object> map) {
		int i=mapper.updateStatus(map);
		return i;
	}
	/**
	 * 批量审核通过和拒绝
	 */
	@Override
	public int updateStatusList(Map<String, Object> map) {
		int i=mapper.updateStatusList(map);
		return i;
	}
	/**
	 * 黑名单调用风控接口
	 * @param contIdCard
	 */
	public String riskBlankName(List<Map<String,String>> cardId){
		logger.info("调用风控黑名单接口url==>{}",riskBlackName);
        logger.info("调用风控黑名单接口入参==>{}");
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("listManages",cardId);
        String result=null;
		try {
			System.out.println(JSONObject.toJSONString(paramMap));
			result = HttpUtils.doPostStr(riskBlackName + "v1/api/renew_loan/user_black_list/add",JSONObject.toJSONString(paramMap));
			if (StringUtils.isBlank(result)) {
				throw new RuntimeException("参数异常!");
			}
			if (!"200".equals(JsonUtil.jsonToMap(result).get("code"))) {
				throw new RuntimeException("接口网络异常!");
			}
			 logger.info("调用风控黑名单接口返回值==>{}",result);
		} catch (Exception e) {
			logger.error("调用风控黑名单接口异常==>",e);
		}
		return result;
	}
}
